package com.acme.bankapp.domain.bank;

import com.acme.exceptions.ClientExistsException;
import com.acme.exceptions.ClientNumberException;


public class Bank {
	private int numberOfClients = 0;
	private final Client[] clients = new Client [10];
	private ClientRegistrationListener[] listeners;
	
	public Bank(ClientRegistrationListener[] listeners) {
		this.listeners = listeners;
	}
	public Bank() {
	}

	public void addClient(Client client)
			throws ClientExistsException, ClientNumberException  {
		if (numberOfClients>10){
			throw new ClientNumberException("Error adding client: " +
					"clients>10");
		} else {
			for(Client cl : clients) {
				if ( client.equals(cl) ){
					throw new ClientExistsException();
				}
			}
			clients[numberOfClients++] = client;
			for(int i = 0; i<listeners.length; i++){
				listeners[i].onClientAdded(client);
			}
		}
	}

	public int getNumberOfClients() {
		return numberOfClients;
	}
	
	public Client getClient(int i) throws ClientNumberException{
		if (i>10) {
			throw new ClientNumberException("Error geting clients! " +
					"(too many)");
		} else if (i<=0) {
			throw new ClientNumberException("Error geting clients! " +
					"(wrong value or no clients in the bank)");
		} else {
			return clients[i-1];
		}
	}
}
